/********************************************************************************
* File Name:	dbconnection.h
* Description:	第十一章实例训练
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
********************************************************************************/
#ifndef DBCONNECTION_H
#define DBCONNECTION_H

#include <QSqlDatabase>
#include <QString>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>

static bool connectiondatabase()
{
    QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("foodorder");
    db.setUserName("user");
    db.setPassword("12345");
    if(!db.open())
    {
        //----------------------输出数据库连接错误信息--------------------//
        qDebug()<<"Failed to open FoodOrder database:\n\t"+db.lastError().driverText();
        qDebug()<<"\t"+db.lastError().databaseText();
        return false;
    }
    /*
    QSqlQuery query;

    //insert  food
    query.exec(QObject::tr("insert into food(fname,ftype,fprice,fhot_count,flag) values('烧茄子','盖饭',5,11,0)"));
    query.exec(QObject::tr("insert into food(fname,ftype,fprice,fhot_count,flag) values('烧土豆','盖饭',5,10,0)"));
    query.exec(QObject::tr("insert into food(fname,ftype,fprice,fhot_count,flag) values('红烧排骨','盖饭',7,1,0)"));
    query.exec(QObject::tr("insert into food(fname,ftype,fprice,fhot_count,flag) values('红烧云豆','盖饭',7,13,0)"));
    query.exec(QObject::tr("insert into food(fname,ftype,fprice,fhot_count,flag) values('蛋炒饭','盒饭',7,13,1)"));
    query.exec(QObject::tr("insert into food(fname,ftype,fprice,fhot_count,flag) values('西红柿','盒饭',7,13,1)"));
    query.exec(QObject::tr("insert into food(fname,ftype,fprice,fhot_count,flag) values('麻婆豆腐','菜系',7,13,2)"));
    query.exec(QObject::tr("insert into food(fname,ftype,fprice,fhot_count,flag) values('炒鸡蛋','菜系',7,13,2)"));
    query.exec(QObject::tr("insert into food(fname,ftype,fprice,fhot_count,flag) values('大米','主食',7,13,3)"));
    query.exec(QObject::tr("insert into food(fname,ftype,fprice,fhot_count,flag) values('面条','主食',7,13,3)"));
    */

    return true;
}



#endif // DBCONNECTION_H
